<template>
  <div class="app-container">
    <el-card class="box-card">
      <div class="flex justify-between">
        <el-button size="default" type="primary" @click="handleAdd">新增装饰</el-button>
        <div class="flex justify-end">
          <div class="flex items-center mr-4">
            <span class="item">装饰名称：</span>
            <el-input v-model="listQuery.title" clearable placeholder="请输入装饰名称" size="normal"></el-input>
          </div>
          <div class="flex items-center mr-4">
            <span class="">装饰分类：</span>
            <el-select v-model="listQuery.type_id" clearable filterable placeholder="请选择" value-key="">
              <el-option v-for="item in options"
                         :key="item.id"
                         :label="item.title"
                         :value="item.id"
              >
              </el-option>
            </el-select>
          </div>
          <el-button size="default" type="primary" @click="getTableData">搜索</el-button>
          <el-button size="default" type="primary" @click="handleReset">重置</el-button>
        </div>
      </div>
      <el-table
        ref="multipleTable"
        v-loading="loading"
        :data="tableData"
        border
        class="mt-3"
        stripe
        style="width: 100%"
        tooltip-effect="dark"
      >
        <el-table-column :index="indexMethod" label="序号" prop="user_code" type="index" width="100px"></el-table-column>
        <el-table-column label="装饰名称" prop="title"></el-table-column>
        <el-table-column label="装饰类别" prop="type_id"></el-table-column>
        <el-table-column label="有效天数" prop="time"></el-table-column>
        <el-table-column label="展示图片" prop="picture">
          <template slot-scope="scoped">
            <video
              v-if="scoped.row.picture && scoped.row.picture.includes('.mp4')"
              :src="scoped.row.picture"
              autoplay class="imageStyle" controls
              muted
            ></video>
            <img v-else :src="scoped.row.picture" alt="" class="imageStyle"/>
          </template>
        </el-table-column>
        <el-table-column label="动态效果" prop="special">
          <template slot-scope="scoped">
            <video
              v-if="scoped.row.special && scoped.row.special.includes('.mp4')"
              :src="scoped.row.special"
              autoplay class="imageStyle" controls
              muted
            ></video>
            <img v-else :src="scoped.row.special" alt="" class="imageStyle"/>
          </template>
        </el-table-column>

        <el-table-column label="情侣头像框" prop="">
          <template slot-scope="scoped">
            <video
              v-if="scoped.row.avatar_frame && scoped.row.avatar_frame.includes('.mp4')"
              :src="scoped.row.avatar_frame"
              autoplay class="imageStyle" controls
              muted
            ></video>
            <img v-else :src="scoped.row.avatar_frame" alt="" class="imageStyle"/>
          </template>
        </el-table-column>
        <el-table-column label="头像框特效" prop="">
          <template slot-scope="scoped">
            <video
              v-if="scoped.row.avatar_special && scoped.row.avatar_special.includes('.mp4')"
              :src="scoped.row.avatar_special"
              autoplay class="imageStyle" controls
              muted
            ></video>
            <img v-else :src="scoped.row.avatar_special" alt="" class="imageStyle"/>
          </template>
        </el-table-column>
        <el-table-column label="主页图片" prop="">
          <template slot-scope="scoped">
            <video
              v-if="scoped.row.minor_picture && scoped.row.minor_picture.includes('.mp4')"
              :src="scoped.row.minor_picture"
              autoplay class="imageStyle" controls
              muted
            ></video>
            <img v-else :src="scoped.row.minor_picture" alt="" class="imageStyle"/>
          </template>
        </el-table-column>
        <el-table-column label="主页头像框" prop="">
          <template slot-scope="scoped">
            <video
              v-if="scoped.row.minor_avatar && scoped.row.minor_avatar.includes('.mp4')"
              :src="scoped.row.minor_avatar"
              autoplay class="imageStyle" controls
              muted
            ></video>
            <img v-else :src="scoped.row.minor_avatar" alt="" class="imageStyle"/>
          </template>
        </el-table-column>

        <el-table-column label="底图图片" prop="bottom_img">
          <template slot-scope="scoped">
            <img :src="scoped.row.bottom_img" alt="" class="imageStyle"/>
          </template>
        </el-table-column>
        <el-table-column label="底图颜色" prop="border_color"></el-table-column>


        <el-table-column label="状态" prop="is_sold">
          <template slot-scope="scoped">
            <el-tag v-if="scoped.row.is_sold === 1" type="success">上架</el-tag>
            <el-tag v-if="scoped.row.is_sold === 0" type="danger">下架</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="添加时间" prop="created_at"></el-table-column>

        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleEdit(scope.row)"
            >编辑
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleGive(scope.row)"
            >赠送
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <cus-pagination
        v-show="total > 0"
        :limit.sync="listQuery.per_page"
        :page.sync="listQuery.page"
        :total="total"
        @pagination="getTableData"
      />
    </el-card>

    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="30%"
      @close="closeDialog('form')"
    >
      <el-form ref="form" :inline="false" :model="formData" :rules="rules1" label-width="120px" size="normal">
        <el-form-item label="选择类别" prop="type_id">
          <el-select v-model="formData.type_id" clearable filterable placeholder="请选择" value-key="">
            <el-option v-for="item in options"
                       :key="item.id"
                       :label="item.title"
                       :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="装扮名称" prop="title">
          <el-input
            v-model="formData.title"
            placeholder="请输入装扮名称"
          />
        </el-form-item>
        <el-form-item label="字体颜色" prop="font">
          <el-input
            v-model="formData.font"
            placeholder="请输入字体颜色"
          />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input
            v-model="formData.sort"
            placeholder="请输入排序"
          />
        </el-form-item>
        <!-- <el-form-item label="装扮介绍" prop="introduction">
            <el-input
                type="textarea"
                rows="3"
                v-model="formData.introduction"
                placeholder="请输入装扮介绍"
                resize="none"
            />
        </el-form-item> -->
        <el-form-item label="展示图片" prop="picture">
          <el-upload
            ref="upload"
            :auto-upload="true"
            :before-upload="beforeAvatarUpload"
            :file-list="fileList1"
            :http-request="customizeUpload"
            :limit="1"
            :on-remove="handlePictureRemove1"
            :on-success="handlePictureSuccess1"
            action=""
            class="upload-demo"
            list-type="picture"
          >
            <el-button v-if="!formData.picture" slot="trigger" size="small" type="primary">上传展示图片</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="动态特效" prop="special">
          <el-upload
                v-if="!formData.special.includes('.mp4')"
                ref="upload"
                :auto-upload="true"
                :before-upload="beforeAvatarUpload"
                :file-list="fileList2"
                :http-request="customizeUpload"
                :limit="1"
                :on-remove="handlePictureRemove2"
                :on-success="handlePictureSuccess2"
                action=""
                class="upload-demo"
                list-type="picture"
            >
                <el-button v-if="!formData.special" slot="trigger" size="small" type="primary">上传动态特效</el-button>
            </el-upload>
            <div v-else class="flex">
                <video :src="formData.special" class="video mr-4" autoplay muted controls></video>
                <el-button type="danger" size="default" @click="handlePictureRemove2" class="danger">删除</el-button>
            </div>
        </el-form-item>
        <el-form-item label="情侣头像框" prop="avatar_frame">
          <el-upload
                v-if="!formData.avatar_frame.includes('.mp4')"
                ref="upload"
                :auto-upload="true"
                :before-upload="beforeAvatarUpload"
                :http-request="customizeUpload"
                :on-remove="handlePictureRemove3"
                :on-success="handlePictureSuccess3"
                action=""
                class="upload-demo"
                list-type="picture"
                :file-list="fileList3"
            >
                <el-button v-if="!formData.avatar_frame" slot="trigger" size="small" type="primary">上传情侣头像框</el-button>
            </el-upload>
            <div v-else class="flex">
                <video :src="formData.avatar_frame" class="video mr-4" autoplay muted controls></video>
                <el-button type="danger" size="default" @click="handlePictureRemove3" class="danger">删除</el-button>
            </div>
        </el-form-item>

        <el-form-item label="头像特效" prop="avatar_special">
          <el-upload
                v-if="!formData.avatar_special.includes('.mp4')"
                ref="upload"
                :auto-upload="true"
                :before-upload="beforeAvatarUpload"
                :http-request="customizeUpload"
                :on-remove="handlePictureRemove4"
                :on-success="handlePictureSuccess4"
                action=""
                class="upload-demo"
                list-type="picture"
                :file-list="fileList4"
            >
                <el-button v-if="!formData.avatar_special" slot="trigger" size="small" type="primary">上传头像特效</el-button>
            </el-upload>
            <div v-else class="flex">
                <video :src="formData.avatar_special" class="video mr-4" autoplay muted controls></video>
                <el-button type="danger" size="default" @click="handlePictureRemove4" class="danger">删除</el-button>
            </div>
        </el-form-item>

        <el-form-item label="主页图片" prop="minor_picture">
          <el-upload
                v-if="!formData.minor_picture.includes('.mp4')"
                ref="upload"
                :auto-upload="true"
                :before-upload="beforeAvatarUpload"
                :http-request="customizeUpload"
                :on-remove="handlePictureRemove5"
                :on-success="handlePictureSuccess5"
                action=""
                class="upload-demo"
                list-type="picture"
                :file-list="fileList5"
            >
                <el-button v-if="!formData.minor_picture" slot="trigger" size="small" type="primary">上传主页图片</el-button>
            </el-upload>
            <div v-else class="flex">
                <video :src="formData.minor_picture" class="video mr-4" autoplay muted controls></video>
                <el-button type="danger" size="default" @click="handlePictureRemove5" class="danger">删除</el-button>
            </div>
        </el-form-item>

        <el-form-item label="主页头像框" prop="">
          <el-upload
                v-if="!formData.minor_avatar.includes('.mp4')"
                ref="upload"
                :auto-upload="true"
                :before-upload="beforeAvatarUpload"
                :http-request="customizeUpload"
                :on-remove="handlePictureRemove6"
                :on-success="handlePictureSuccess6"
                action=""
                class="upload-demo"
                list-type="picture"
                :file-list="fileList6"
            >
                <el-button v-if="!formData.minor_avatar" slot="trigger" size="small" type="primary">上传主页头像框</el-button>
            </el-upload>
            <div v-else class="flex">
                <video :src="formData.minor_avatar" class="video mr-4" autoplay muted controls></video>
                <el-button type="danger" size="default" @click="handlePictureRemove6" class="danger">删除</el-button>
            </div>
        </el-form-item>

        <el-form-item label="底图图片" prop="bottom_img">
          <el-upload
            ref="upload"
            :auto-upload="true"
            :before-upload="beforeAvatarUpload"
            :file-list="fileList7"
            :http-request="customizeUpload"
            :limit="1"
            :on-remove="handlePictureRemove7"
            :on-success="handlePictureSuccess7"
            action=""
            class="upload-demo"
            list-type="picture"
          >
            <el-button v-if="!formData.bottom_img" slot="trigger" size="small" type="primary">上传图片</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="底图颜色" prop="border_color">
          <el-input
            v-model.number="formData.border_color"
            placeholder="请输入底图颜色"
          >
          </el-input>
        </el-form-item>

        <el-form-item label="有效天数" prop="time">
          <el-input
            v-model="formData.time"
            placeholder="请输入有效天数"
          >
            <template slot="append">天</template>
          </el-input>
        </el-form-item>
        <el-form-item label="绑定礼物ID" prop="">
          <el-input
            v-model.number="formData.gift_id"
            placeholder="请输入绑定的礼物ID"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="状态" prop="is_sold">
          <el-radio-group v-model="formData.is_sold">
            <el-radio :label="1">
              上架
            </el-radio>
            <el-radio :label="0">
              下架
            </el-radio>
          </el-radio-group>

        </el-form-item>
      </el-form>

      <span slot="footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="handleOnSubmit">确定</el-button>
            </span>
    </el-dialog>

    <el-dialog
      :visible.sync="giveDialogVisible"
      title="赠送装饰"
      width="30%"
      @close="closeDialog('form1')"
    >
      <el-form ref="form1" :inline="false" :model="giveFormData" :rules="rules" label-width="120px" size="normal">
        <el-form-item label="赠送用户编号" prop="user_code">
          <el-input v-model="giveFormData.user_code" placeholder="请输入用户编号"></el-input>
        </el-form-item>
        <el-form-item label="赠送天数" prop="count">
          <el-input v-model="giveFormData.count" placeholder="请输入赠送天数"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer">
                <el-button @click="giveDialogVisible = false">取消</el-button>
                <el-button type="primary" @click="handleSubmit">确认</el-button>
            </span>
    </el-dialog>

  </div>
</template>

<script>
import {
  addDecorateProduct,
  decorateType,
  delDecorateProduct,
  editDecorateProduct,
  giveDecorateProduct,
  listData
} from '@/api/decorate'

export default {
  name: 'decorate',
  components: {},
  data() {
    return {
      dialogVisible: false,
      dialogTitle: undefined,
      formData: {
        type_id: undefined,
        title: undefined,
        picture: undefined,
        special: '',
        time: undefined,
        is_sold: undefined,
        avatar_frame: '',
        avatar_special: '',
        minor_picture: '',
        minor_avatar: '',
        gift_id: undefined,
        font: undefined,
        sort: undefined,
        bottom_img: undefined
      },
      rules1: {
        type_id: [{ required: true, message: '请选择装饰分类', trigger: 'blur' }],
        title: [{ required: true, message: '请输入装饰名称', trigger: 'blur' }],
        picture: [{ required: true, message: '请上传展示图片', trigger: 'blur' }],
        special: [{ required: true, message: '请上传动态效果', trigger: 'blur' }],
        time: [{ required: true, message: '请输入有效天数', trigger: 'blur' }],
        is_sold: [{ required: true, message: '请选择状态', trigger: 'blur' }]
      },
      loading: false,
      tableData: [],
      listQuery: {
        page: 1,
        per_page: 10
      },
      total: 1,
      options: [],
      fileList1: undefined,
      fileList2: undefined,
      fileList3: undefined,
      fileList4: undefined,
      fileList5: undefined,
      fileList6: undefined,
      fileList7: undefined,
      giveDialogVisible: false,
      giveFormData: {
        user_code: undefined,
        count: undefined,
        id: undefined
      },
      rules: {
        user_code: [{ required: true, message: '请输入用户编号', trigger: 'blur' }],
        count: [{ required: true, message: '请输入赠送天数', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getDecorateType()
    this.getTableData()
  },
  methods: {
    indexMethod(index) {
      return this.total - (this.listQuery.page - 1) * this.listQuery.per_page - index
    },
    // 网络请求
    /**
     * getTableData
     * @desc 获取表格数据
     */
    async getTableData() {
      this.loading = true
      for (let key in this.listQuery) {
        this.listQuery[key] = this.listQuery[key] === '' ? undefined : this.listQuery[key]
      }
      const detail = await listData(this.listQuery)
      console.log(this.listQuery.id)
      if (detail.code === 200) {
        this.tableData = detail.data.list
        this.total = detail.data.meta.total
      }
      this.loading = false

    },
    async getDecorateType() {
      const { data, code } = await decorateType({
        page: 1,
        per_page: 1000
      })
      if (code === 200) {

        this.options = data.list
        this.total = data.meta.total
      }
    },
    closeDialog(form) {
      if (this.$refs[form]) {
        this.$refs[form].clearValidate() // 清除整个表单的校验
        this.$refs[form].resetFields() // 清除整个表单的校验
      }
      this.fileList1 = []
      this.fileList2 = []
      this.fileList3 = []
      this.fileList4 = []
      this.fileList5 = []
      this.fileList6 = []
      this.fileList7 = []
      this.formData = {
        type_id: undefined,
        title: undefined,
        picture: undefined,
        special: '',
        time: undefined,
        is_sold: undefined,
        avatar_frame: '',
        avatar_special: '',
        minor_picture: '',
        minor_avatar: '',
        gift_id: undefined,
        font: undefined,
        sort: undefined,
        bottom_img: undefined
      }
      this.giveFormData = {
        user_code: undefined,
        count: undefined,
        id: undefined
      }
    },

    handleOnSubmit() {

      this.$refs['form'].validate(valid => {
        if (valid) {

          for (let key in this.formData) {
            this.formData[key] = !this.formData[key] ? '' : this.formData[key]
          }
          if(!this.formData.gift_id) {
            this.formData.gift_id = 0
          }
          if (this.formData.id) {
            editDecorateProduct(this.formData).then(res => {
              if (res.code === 200) {
                this.$message({
                  type: 'success',
                  message: '操作成功'
                })
              }
              this.dialogVisible = false
              this.getTableData()
            })
          } else {
            addDecorateProduct(this.formData).then(res => {
              if (res.code === 200) {
                this.$message({
                  type: 'success',
                  message: '操作成功'
                })
              }
              this.dialogVisible = false
              this.getTableData()
            })
          }
        }
      })
    },
    handleEdit(row) {
      this.dialogTitle = '编辑装饰'
      this.dialogVisible = true
      this.formData = JSON.parse(JSON.stringify(row))
      if(!this.formData.special) {
        this.formData.special = ''
      }
      if(!this.formData.avatar_frame) {
        this.formData.avatar_frame = ''
      }
      if(!this.formData.avatar_special) {
        this.formData.avatar_special = ''
      }
      if(!this.formData.minor_picture) {
        this.formData.minor_picture = ''
      }
      if(!this.formData.minor_avatar) {
        this.formData.minor_avatar = ''
      }
      this.fileList1 = [{
        url: this.formData.picture
      }]
      this.fileList2 = [{
        url: this.formData.special
      }]
      if (this.formData.avatar_frame) {

        this.fileList3 = [{
          url: this.formData.avatar_frame
        }]
      }
      if (this.formData.avatar_special) {

        this.fileList4 = [{
          url: this.formData.avatar_special
        }]
      }
      if (this.formData.minor_picture) {

        this.fileList5 = [{
          url: this.formData.minor_picture
        }]
      }
      if (this.formData.minor_avatar) {

        this.fileList6 = [{
          url: this.formData.minor_avatar
        }]
      }
      if (this.formData.bottom_img) {

        this.fileList7 = [{
          url: this.formData.bottom_img
        }]
      }
    },
    handleGive(row) {
      this.giveFormData.id = row.id
      this.giveDialogVisible = true
    },
    handleSubmit() {
      this.$refs['form1'].validate(valid => {
        if (valid) {
          giveDecorateProduct(this.giveFormData).then(res => {
            if (res.code === 200) {
              this.$message({
                type: 'success',
                message: '操作成功'
              })
            }
            this.giveDialogVisible = false
            this.getTableData()
          })

        }
      })
    },
    handleDelete(row) {
      this.$confirm('确定删除该装饰？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delDecorateProduct(row.id).then(res => {
          if (res.code === 200) {
            this.$message({
              type: 'success',
              message: '操作成功'
            })
            this.getTableData()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleReset() {
      this.listQuery = {
        page: 1,
        per_page: 10
      }
      this.getTableData()
    },
    handleAdd() {
      this.dialogTitle = '新增装饰'
      this.dialogVisible = true
    },
    beforeAvatarUpload(file) {
      // const isJPG = file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/webp' || file.type === 'image/gif'
      // const isLt2M = file.size / 1024 / 1024 < 2

      // if (!isJPG) {
      //     this.$message.error('请上传JPG/PNG格式的图片!')
      // }
      // if (!isLt2M) {
      //     this.$message.error('上传图片大小不能超过 2MB!')
      // }
      // return isJPG && isLt2M
    },
    handlePictureSuccess1() {
      this.formData.picture = this.ossLastImgUrl
    },
    handlePictureRemove1() {
      this.fileList1 = []
      this.formData.picture = undefined
    },
    handlePictureSuccess2() {
      this.formData.special = this.ossLastImgUrl
    },
    handlePictureRemove2() {
      this.fileList2 = []
      this.formData.special = ''
    },
    handlePictureSuccess3() {
      this.formData.avatar_frame = this.ossLastImgUrl
    },
    handlePictureRemove3() {
      this.fileList3 = []
      this.formData.avatar_frame = ''
    },
    handlePictureSuccess4() {
      this.formData.avatar_special = this.ossLastImgUrl
    },
    handlePictureRemove4() {
      this.fileList4 = []
      this.formData.avatar_special = ''
    },
    handlePictureSuccess5() {
      this.formData.minor_picture = this.ossLastImgUrl
    },
    handlePictureRemove5() {
      this.fileList5 = []
      this.formData.minor_picture = ''
    },
    handlePictureSuccess6() {
      this.formData.minor_avatar = this.ossLastImgUrl
    },
    handlePictureRemove6() {
      this.fileList6 = []
      this.formData.minor_avatar = ''
    },
    handlePictureSuccess7() {
      this.formData.bottom_img = this.ossLastImgUrl
    },
    handlePictureRemove7() {
      this.fileList7 = []
      this.formData.bottom_img = undefined
    },
  }
}
</script>

<style lang="scss" scoped>
.infoBox {
  margin-top      : 15px;
  margin-bottom   : 15px;
  padding-bottom  : 15px;
  border          : 1px solid #999999;
  border-radius   : 10px;
  display         : flex;
  align-items     : baseline;
  justify-content : center;
  box-sizing      : border-box;
  padding-right   : 30px;

  .info_1 {
    display         : flex;
    justify-content : center;
    flex-direction  : column;
    width           : 100%;

    .info_2 {
      margin-top  : 15px;
      display     : flex;
      align-items : center;
      position    : relative;

      span {
        width      : 180px;
        text-align : right;
      }

      .tip {
        color     : red;
        font-size : 12px;
        position  : absolute;
        top       : 100%;
        left      : 120px;
      }
    }
  }
}

.item {
  width : 100px;
}

.formItemBox {
  display     : flex;
  align-items : center;
  flex-wrap   : wrap;

  width       : 100%;
  overflow    : hidden;
}

::v-deep {
  .formItemBox {
    .el-form-item {
      width : 50% !important;
    }
  }

}

.imageStyle {
  width : 200px;
}
.video{
    width: 200px;
}
.danger {
    height: max-content;
}
</style>
